package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.HealthUpLoadService;
import com.itheima.utils.POIUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/upload")
public class HealthUpLoadController {


    @Reference
    private HealthUpLoadService healthUpLoadService;

    //分页查询
    @RequestMapping("/findByPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pg = healthUpLoadService.pageQuery(queryPageBean);
        return pg;
    }

    //新增
    @RequestMapping("/add")
    public Result edit(@RequestBody ReportList reportList) {
        try {
            healthUpLoadService.add(reportList);
        } catch (Exception e) {
            return new Result(false,"新增失败");
        }
        return new Result(true, "新增成功");
    }

//    //查询该手机号是否存在
//    @RequestMapping("/selectPhone")
//    public Result selectPhone(@Param("phoneNumber") String phoneNumber){
//        healthUpLoadService.findByPhone()
//        return new Result(false,"该手机号不存在，请重新确认！");
//    }


    //根据手机号查询信息
    @RequestMapping("/findByPhone")
    public Result findByPhone(@Param("phoneNumber") String phoneNumber){
        try {
            ReportList report = healthUpLoadService.findByPhone(phoneNumber);
            if(report==null){
                return new Result(false,"该用户不存在，请重新核对手机号！");
            }
            int count = healthUpLoadService.findByPhoneFromReportlist(phoneNumber);
            //先判断查询结果是否为0，如果为空证明没有这个手机号
            if(count>0){
                return new Result(false,"该用户在体检表中已经存在!");
            }
            return new Result(true,"查询成功",report);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"查询失败");
        }
    }




    //删除
    @RequestMapping("/delete")
    public Result delete(Integer[] id) {
        boolean delete = false;
        try {
            delete = healthUpLoadService.deletes(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(delete){
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        }else{
            return new Result(false, "有会员已经提交体检报告，删除失败");
        }
    }
    //上传文件
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> readExcel = POIUtils.readExcel(excelFile);
            if(readExcel!=null && readExcel.size()>0){
                List<Examination> list = new ArrayList<>();
                for (String[] strings : readExcel) {
                    Examination examination = new Examination(0,
                            strings[0],
                            strings[1],
                            strings[2],
                            strings[3],
                            strings[4],
                            strings[5],
                            new Date(strings[6]),
                            strings[7],
                            strings[8],
                            strings[9],
                            strings[10],
                            strings[11],
                            strings[12],
                            strings[13],
                            strings[14],
                            strings[15],
                            strings[16],
                            strings[17],
                            strings[18],
                            strings[19],
                            strings[20],
                            strings[21],
                            strings[22],
                            strings[23],
                            strings[24],
                            strings[25]
                    );
                    list.add(examination);
                }
                healthUpLoadService.addExcel(list);
                return new Result(true,"导入数据成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false,"导入数据失败");
    }
}
